#pragma once
#include <thread>

#include "command.h"
class IEPE_Command : public QCommand
{
    Q_OBJECT
    
public:
    IEPE_Command(QTcpSocket* socket):QCommand(socket){
        std::thread([=]{
            try{
                while(socket->isValid()){
                    if(socket->bytesAvailable()>0){
                        QByteArray header = socket->read(8);
                        if((*(unsigned int*)header.mid(0,4).data())==0xaa00ff55){
                            unsigned int len = *(unsigned int*)header.mid(4,4).data();
                            QByteArray data = socket->read(len);
                            if(len==1){
                                qDebug() << "command " << (data.at(0)==0x21?"success":"fail");
                            }else{
                                qDebug() << "received data from client:" << data.toStdString().c_str();
                            }
                        }else{
                            qDebug() << "data shift error";
                        }
                    }else{
                        std::this_thread::sleep_for(std::chrono::milliseconds(500));
                    }
                }
            }catch(std::exception e){}
        }).detach();
    }
    void query(){command(1,&std::vector<char>{0x10}[0]);qDebug() <<"query command";};
    void idle(){command(1,&std::vector<char>{0x20}[0]);qDebug() <<"idle command";};
    void gather(){command(1,&std::vector<char>{0x30}[0]);qDebug() <<"gather command";};
    void settings_gather(){command(59,&std::vector<char>{0x40}[0]);qDebug() <<"settings_gather command";};

    void calibration(){command(272,(char*)&std::vector<char>{0x47}[0]);qDebug() <<"calibration command";};
    void setting_param(){command(6,&std::vector<char>{0x4a}[0]);qDebug() <<"setting_param command";};
    void send_gather(){command(3,&std::vector<char>{0x50}[0]);qDebug() <<"send_gather command";};
    void flash_read(){command(1,&std::vector<char>{0x6c}[0]);qDebug() <<"flash_read command";};
};
